package cn.dnui_xcz826.dnuifood_16110100826.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CollectBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ResultBean;
import cn.dnui_xcz826.dnuifood_16110100826.controller.Constants;
import cn.dnui_xcz826.dnuifood_16110100826.controller.FoodInfoActivity;
import cn.dnui_xcz826.dnuifood_16110100826.controller.FoodListActivity;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.CollectModel;


/**
 * Created by Administrator on 2018/11/12.
 */
public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.ViewHolder> implements onRetrofitListener {
    private CollectModel collectModel;
    private String shop_id="",food_id="",user_id="";
    private Context context;
    private List<CollectBean> data;
    private LayoutInflater layoutInflater;

    public CollectAdapter(Context context, List data, int layoutResource) {
        this.context=context;
        this.data=data;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.collect_item, viewGroup, false);
        return new CollectAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CollectAdapter.ViewHolder holder, int position) {
        final CollectBean entity = (CollectBean) data.get(position);
        //user_id=entity.getUser_id()+"";
        if ("0".equals(entity.getFlag()+""))//店铺
        {
//            holder.collect_shopid.setText(entity.getShop_id());
//            holder.collect_foodid.setText(entity.getFood_id());
            holder.collect_name.setText(entity.getShopname());
            holder.collect_content.setText(entity.getAddress());
            if (!entity.getPic().equals(""))
            {
                Picasso.with(context).load(Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);
            }
            holder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    //跳转至店铺详细页
                    Intent intent = new Intent(context, FoodListActivity.class);
                    intent.putExtra("shop_id",((CollectBean) data.get(position)).getShop_id()+"");
                    intent.putExtra("shop_name",((CollectBean) data.get(position)).getShopname()+"");
                    context.startActivity(intent);
                }
            });
            holder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    uncollectShop(entity.getShop_id()+"");
                }
            });
        }

        else if ("1".equals(entity.getFlag()+""))//菜品
        {
            holder.collect_name.setText(entity.getFoodname()+"");
            holder.collect_content.setText(entity.getPrice() + "元");
            if (!entity.getPic().equals("")) {
                Picasso.with(context).load(Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);
            }
            holder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转至菜谱详细页
                    Intent intent = new Intent(context, FoodInfoActivity.class);
                    intent.putExtra("food_id",((CollectBean) data.get(position)).getFood_id()+"");
                    context.startActivity(intent);
                }
            });
            holder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uncollectFood(entity.getFood_id()+"");
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView collect_pic;
        private TextView collect_name, collect_content, collect_shopid, collect_foodid;
        private Button collect_enter, collect_cancle;
        public ViewHolder(View itemView) {
            super(itemView);
            collect_pic = (ImageView) itemView.findViewById(R.id.collect_pic);
            collect_name = (TextView) itemView.findViewById(R.id.food_name);
            collect_content = (TextView) itemView.findViewById(R.id.collect_content);
            collect_enter = (Button) itemView.findViewById(R.id.collect_enter);
            collect_cancle = (Button) itemView.findViewById(R.id.collect_cancle);
        }
    }
    private void uncollectShop(String Shop_id) {
        //取消收藏店铺
        this.shop_id=Shop_id;
        CollectModel collectModel=new CollectModel();
        collectModel.userCollectShop(user_id, Shop_id,this);
    }
    private void uncollectFood(String Food_id) {
        //取消收藏菜谱
        this.food_id=Food_id;
        CollectModel collectModel=new CollectModel();
        collectModel.userCollectFood(user_id, Food_id,this);
    }
    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag) {
            case Constants.USERCOLLECTSHOP:
                //处理取消收藏店铺 userCollectShop
                if (((ResultBean)o).getSuccess().equals("1")) {
                    Toast.makeText(context, "取消收藏店铺成功", Toast.LENGTH_SHORT).show();
                    for (int i = 0; i < data.size(); i++) {
                        CollectBean object = (CollectBean) data.get(i);
                        if (String.valueOf(object.getShop_id()).equals(shop_id)) {
                            data.remove(object);
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
                else {
                    Toast.makeText(context, "取消收藏店铺失败", Toast.LENGTH_SHORT).show();
                }


            case Constants.USERCOLLECTFOOD:
                //处理取消收藏菜谱 userCollectFood
                if (((ResultBean)o).getSuccess().equals("1")) {
                    Toast.makeText(context, "取消收藏菜品成功", Toast.LENGTH_SHORT).show();
                    for (int i = 0; i < data.size(); i++) {
                        CollectBean object = (CollectBean) data.get(i);
                        if (String.valueOf(object.getFood_id()).equals(food_id)) {
                            data.remove(object);
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
                else {
                    Toast.makeText(context, "取消收藏菜品失败", Toast.LENGTH_SHORT).show();
                }
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(context, "网络发生错误", Toast.LENGTH_SHORT).show();
    }

}